<?php
/**
 * 基础物料Lib类
 * @author xiayuanjun@shopex.cn
 * @version 1.0
 */
class material_basic_material{

     function __construct(){
        $this->_basicMaterialObj = app::get('material')->model('basic_material');
        $this->_basicMaterialExtObj = app::get('material')->model('basic_material_ext');
        $this->_basicMaterialStockObj = app::get('material')->model('basic_material_stock');
     }

    /**
     * 自有仓储库存查询，获取有仓库库存的物料列表显示
     * @param string $row 物料是否可见字段
     * @return string
     */
    function countAnother($filter=null){
        $other_table_name = app::get('ome')->model('branch_product')->table_name(1);
        $count = ' COUNT(*) ';
        if (isset($filter['product_group'])){
            $count = ' COUNT( DISTINCT '.$this->_basicMaterialObj->table_name(1).'.product_id ) ';
        }
        
        $strWhere = '';
        
        if(isset($filter['branch_id'])){
            if (is_array($filter['branch_id'])){
                $strWhere = ' AND '.$other_table_name.'.branch_id IN ('.implode(',', $filter['branch_id']).') ';
            }else {
                $strWhere = ' AND '.$other_table_name.'.branch_id = '.$filter['branch_id'];
            }
        }else{
            if ($filter['branch_ids']) {
                if (is_array($filter['branch_ids'])){
                    $strWhere = ' AND '.$other_table_name.'.branch_id IN ('.implode(',', $filter['branch_ids']).') ';
                }else {
                    $strWhere = ' AND '.$other_table_name.'.branch_id = '.$filter['branch_ids'];
                }
            }
        }
        $sql = 'SELECT '.$count.'as _count FROM `'.$this->_basicMaterialObj->table_name(1).'` LEFT JOIN  '.$other_table_name.'  ON '.$this->_basicMaterialObj->table_name(1).'.bm_id = '.$other_table_name.'.product_id WHERE '.$this->_basicMaterialObj->_filter($filter) . $strWhere;

        $row = $this->_basicMaterialObj->db->selectrow($sql);

        return intval($row['_count']);
    }

    /**
     * 获取基础物料信息及扩展信息
     * @param intval $bm_id
     * @return string
     */
    public function getBasicMaterialExt($bm_id)
    {
        $basicMateriaItem    = $this->_basicMaterialObj->dump(array('bm_id'=>$bm_id), '*');
        if(empty($basicMateriaItem))
        {
            return '';
        }
        
        #扩展信息
        $basicMateriaExt    = $this->_basicMaterialExtObj->dump(array('bm_id'=>$bm_id), 'cost, retail_price, weight, unit');
        
        #条形码
        $basicMateriaItem['barcode']    = $this->getBasicMaterialCode($bm_id);
        
        return array_merge($basicMateriaItem, $basicMateriaExt);
    }
    
    /**
     * 获取基础物料信息及库存信息
     * @param intval $bm_id
     * @return string
     */
    public function getBasicMaterialStock($bm_id)
    {
        $basicMateriaItem    = $this->_basicMaterialObj->dump(array('bm_id'=>$bm_id), '*');
        if(empty($basicMateriaItem))
        {
            return '';
        }
        
        #库存信息
        $basicMateriaStock    = $this->_basicMaterialStockObj->dump(array('bm_id'=>$bm_id), 'store, store_freeze, alert_store, last_modified');
    
        return array_merge($basicMateriaItem, $basicMateriaStock);
    }
    
    /**
     * 获取基础物料信息及库存信息
     * @param intval $bm_id
     * @return string
     */
    public function getBasicMaterialDetail($bm_id)
    {
        $basicMateriaExt    = $this->getBasicMaterialExt($bm_id);
        if(empty($basicMateriaExt))
        {
            return '';
        }
        
        #库存信息
        $basicMateriaStock    = $this->getBasicMaterialStock($bm_id);
        
        return array_merge($basicMateriaExt, $basicMateriaStock);
    }
    
    /**
     * 获取基础物料对应条形码
     * @param intval $bm_id
     * @return string
     */
    public function getBasicMaterialCode($bm_id)
    {
        $codebaseObj    = app::get('material')->model('codebase');
        $codeBaseLib    = kernel::single('material_codebase');
        
        #基础物料条码
        $codType      = $codeBaseLib->getBarcodeType();
        $code_info    = $codebaseObj->dump(array('bm_id'=>$bm_id, 'type'=>$codType), 'code');
        
        return $code_info['code'];
    }
}
